package com.evan.kdl.test.day02;

import cn.hutool.core.date.StopWatch;
import com.evan.kdl.test.entity.MyTask;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @author huangzhongliang
 * @time 2024/01/18 0:02
 */
public class CompletableFutureDemo {
    public static void main(String[] args) {

        List<MyTask> myTasks = IntStream.range(0, 18).mapToObj(item -> new MyTask(1)).collect(Collectors.toList());
        StopWatch sw = new StopWatch();
        sw.start();
        List<CompletableFuture<Integer>> futures = myTasks.stream().map(m -> CompletableFuture.supplyAsync(m::doWork)).collect(Collectors.toList());
        List<Integer> results = futures.stream().map(CompletableFuture::join).collect(Collectors.toList());
        sw.stop();
        System.out.printf("processed %d tasks cost %.2f second", myTasks.size(), sw.getTotalTimeSeconds());

    }
}
